# -*- coding: utf-8 -*-
import scrapy
from tencent.items import TencentItem


class TencentsSpider(scrapy.Spider):
    name = 'tencents'
    allowed_domains = ['tencent.com']
    start_urls = ['http://hr.tencent.com/position.php']

    def parse(self, response):
        # 1, 先获取第一页的职位信息
        position_list = response.xpath("//tr[@class='even']|//tr[@class='odd']")
        # 2, 遍历职位列表，将所需要的信息进行提取
        # print(position_list)
        for position in position_list:
            item = TencentItem()
            item['name'] = position.xpath("./td[1]/a/text()").extract_first()
            item['classes'] = position.xpath("./td[2]/text()").extract_first()
            item['link'] = response.urljoin(position.xpath("./td[1]/a/@href").extract_first())
            item['num'] = position.xpath("./td[3]/text()").extract_first()
            item['address'] = position.xpath("./td[4]/text()").extract_first()
            item['time'] = position.xpath("./td[5]/text()").extract_first()
            yield item
        # 翻页功能
        next_page = response.xpath("//*[@id='next']/@href").extract_first()
        if next_page != 'javascript:;':
            # 构建完整链接
            url = response.urljoin(next_page)
            # 构建对象
            res = scrapy.Request(
                url = url,
                callback=self.parse
            )
            # 返回请求对象

            yield res